clear all, close all, clc;



init_name;

load(data_base_save_name)

full_data_base = data_base;

edge_set = data_base{1}.edge_set;


%%
clear data_base



%setting manually
n_bag = 1;
n_crank = 35;
for i = 1:6
    belong_id{i} = [];
    belong_id{i} = [belong_id{i} n_bag*(i-1)+1:i*n_bag];
    belong_id{i} = [belong_id{i} n_bag*6+(i-1)*n_crank + 1:n_bag*6+i * n_crank];
end

for test_id = 1:length(full_data_base)
    clear data_base
    data_base{2} = full_data_base{ test_id };
    
    
    sel_classifier_id = [];
    
    if_recommen_matrix = 1;
    
    for append_id = [1:test_id-1 test_id+1:length(full_data_base)]
        
        sel_classifier_id = [sel_classifier_id belong_id{append_id} ];
        if(isempty(data_base{1}))           
            data_base{1} = full_data_base{ append_id };
        else
            
            data_base{1}.name = [   data_base{1}.name ' + ' ...
                                    full_data_base{ append_id }.name];
            data_base{1}.start = [data_base{1}.start ...
                                    full_data_base{ append_id }.start];
            data_base{1}.end = [data_base{1}.end ...
                                    full_data_base{ append_id }.end];
            data_base{1}.rate = [data_base{1}.rate ...
                                    full_data_base{ append_id }.rate];
            data_base{1}.frame_n = [data_base{1}.frame_n ...
                                   + full_data_base{ append_id }.frame_n];
                               
            data_base{1}.score_matrix = [data_base{1}.score_matrix;
                                     full_data_base{ append_id }.score_matrix];
            
            data_base{1}.Fmatrix = [data_base{1}.Fmatrix;
                                     full_data_base{ append_id }.Fmatrix];
            data_base{1}.ZeroOne = [data_base{1}.ZeroOne;
                                     full_data_base{ append_id }.ZeroOne];
            
            for k = 1:length( data_base{1}.global_feat)
                data_base{1}.global_feat{k} = [data_base{1}.global_feat{k};
                                            full_data_base{ append_id }.global_feat{k}];
            end
            
            data_base{1}.corr_pos =     data_base{1}.corr_pos ...
                                    + full_data_base{ append_id }.corr_pos;
            data_base{1}.corr_neg =     data_base{1}.corr_neg ...
                                    + full_data_base{ append_id }.corr_neg;
                                
            try                
                data_base{1}.recommend_matrix = [data_base{1}.recommend_matrix;...
                                     full_data_base{ append_id }.recommend_matrix];
            catch
                disp('warning no recommen_matrix!')
                if_recommen_matrix = 0;
            end
            
                                 
        end        
    end
    
    %remove classifiers training on testing video
    for i = 1:2
        data_base{i}.corr_pos = data_base{i}.corr_pos(...
            sel_classifier_id,sel_classifier_id);
        data_base{i}.corr_neg = data_base{i}.corr_neg(...
            sel_classifier_id,sel_classifier_id);        
        data_base{i}.Fmatrix = data_base{i}.Fmatrix(...
            :,sel_classifier_id);        
        data_base{i}.ZeroOne = data_base{i}.ZeroOne(...
            :,sel_classifier_id);                
        data_base{i}.sel_classifier_id = sel_classifier_id;
    end
        
    %remove the probe related with testing video
    %(using edge_set)
    
    if if_recommen_matrix==1        
        bo = find(int8(edge_set(:, 1)~=test_id).*int8(edge_set(:, 2)~=test_id)>0);
        for i = 1:2
            data_base{i}.recommend_matrix =  data_base{i}.recommend_matrix(:,bo);            
        end        
    end
    
    sub_name = [data_base_save_name '_part' num2str(test_id) '.mat'];
    
    save( sub_name,'data_base');
end
